package com.juzhi.dvrapp.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.transition.Transition;
import android.view.View;

import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.callback.OnTransitionListener;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.Urls;
import com.juzhi.dvrapp.videoplay.utils.CommonUtil;
import com.juzhi.dvrapp.videoplay.utils.OrientationUtils;
import com.juzhi.dvrapp.videoplay.video.base.GSYVideoPlayer;
import com.juzhi.dvrapp.widget.EmptyControlVideo;

import butterknife.BindView;

import static com.juzhi.dvrapp.videoplay.utils.CommonUtil.hideNavKey;

public class DevicePlayActivity extends BaseAcitivity {

    public final static String IMG_TRANSITION = "IMG_TRANSITION";
    public final static String TRANSITION = "TRANSITION";

    @BindView(R.id.empty_video_player)
    private EmptyControlVideo videoPlayer;
    private OrientationUtils orientationUtils;
    private boolean isTransition;
    private Device mDevice;
    private String mSsid;


    @Override
    public int getLayoutId() {
        return R.layout.activity_device_play;
    }

    @Override
    protected void initView() {

        mSsid = getIntent().getStringExtra(Config.SSID);
        DeviceManager deviceManager = DeviceManager.getDeviceManager(this);
        mDevice = deviceManager.getDevice(mSsid);

        isTransition = getIntent().getBooleanExtra(TRANSITION, false);
        initTransition();

        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void playVideo() {
        videoPlayer.setUp(mDevice.getDeviceModel().getDeviceName(),mDevice.getDeviceModel().getSsid());
        orientationUtils = new OrientationUtils(this,videoPlayer);
        orientationUtils.setEnable(false);
        String thumbUrl = Urls.URL_CMS_GET_DEVICE_THUMB + mSsid + "/" + mSsid + "_1.JPEG";
        videoPlayer.loadCoverImage(thumbUrl, R.mipmap.logo);
        videoPlayer.setLockLand(true);
        videoPlayer.setRotateViewAuto(false);  //关闭自动旋转  并不再打开
        videoPlayer.startPlayLogic();
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.release();
    }

    @Override
    protected void onResume() {
        super.onResume();
        CommonUtil.hideSupportActionBar(this, true, true);
        hideNavKey(this);
        videoPlayer.onVideoResume();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {  //activity显示的时候
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {//加了 hasFocus
            hideNavKey(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoPlayer.release();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //释放所有
        videoPlayer.setStandardVideoAllCallBack(null);
        GSYVideoPlayer.releaseAllVideos();
        if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onBackPressed();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                    overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
                }
            }, 500);
        }
    }

    private void initTransition() {
        if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            postponeEnterTransition();
            ViewCompat.setTransitionName(videoPlayer, IMG_TRANSITION);
            addTransitionListener();
            startPostponedEnterTransition();
        } else {
            playVideo();
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private boolean addTransitionListener() {
        Transition transition = getWindow().getSharedElementEnterTransition();
        if (transition != null) {
            transition.addListener(new OnTransitionListener(){
                @Override
                public void onTransitionEnd(Transition transition) {
                    super.onTransitionEnd(transition);
                    playVideo();
                    transition.removeListener(this);
                }
            });
            return true;
        }
        return false;
    }
}
